package com.ply.filter;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @author XiaoPan
 * date: 2022/1/15 14:18
 * <p>
 * action:
 *
 *  两种方法 一样的 获取 getRequest 的本质也是 ThreadLocal 里面的 存储的   并且在最后   会被 spring 自动清除
 *
 *  都一样 在 都是 在request 中  存储 和 取出
 *
 *
 */


public class UserUtils {
    private static final String USERNAME = "userId";
    private static final String MANAGENAME = "manageId";


    public static Integer getUserId(){
//        ThreadLocal<Integer> userIdL = new ThreadLocal<>();
//       return userIdL.get();
       return (Integer) ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest().getAttribute(USERNAME);
    }

     static void setUserId(Integer userId){
//        ThreadLocal<Integer> userIdL = new ThreadLocal<>();
//        userIdL.set(userId);
         ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest().setAttribute(USERNAME,userId);
    }
        public static Integer getAdminId(){
//        ThreadLocal<Integer> userIdL = new ThreadLocal<>();
//       return userIdL.get();
       return (Integer) ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest().getAttribute(MANAGENAME);
    }

     static void setAdminId(Integer adminId){

/**        ThreadLocal<Integer> userIdL = new ThreadLocal<>();
        userIdL.set(userId);
 */
         ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest().setAttribute(MANAGENAME,adminId);
    }




    static void clear(){
//        ThreadLocal<Integer> userIdL = new ThreadLocal<>();
//        userIdL.remove();
    }
}
